package communication;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerConnection extends Connection {

	
	protected Socket socket;
	protected ObjectOutputStream send;
	protected ObjectInputStream receive;

	
	public ServerConnection(Socket socket)
	throws ConnectionException{
		try {		

			this.socket = socket;
		
			receive = new ObjectInputStream(socket.getInputStream());
			send = new  ObjectOutputStream(socket.getOutputStream());

		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}		
	}


	protected void doSend(boolean bool)
	throws ConnectionException{
		try {
			send.writeBoolean(bool);
			send.flush();	
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}	
	}


	protected void doSend(Object obj)
	throws ConnectionException{
		try {
			send.writeObject(obj);
			send.flush();	
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}		
	}


	protected void doSend(String str)
	throws ConnectionException{
		try {
			send.writeUTF(str);
			send.flush();	
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}		
	}


	protected void doSend(long l)
	throws ConnectionException{
		try {
			send.writeLong(l);
			send.flush();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}	
	}


	protected void doSend(byte[] buffer)
	throws ConnectionException {
		try {
			send.write(buffer);
			send.flush();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


	protected void doSend(byte[] buffer, int init, int length)
	throws ConnectionException {
		try {
			send.write(buffer,init,length);
			send.flush();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}		
	}


	protected Object doReceiveObject()
	throws ConnectionException{
		try {
			return (Object) receive.readObject();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		} catch (ClassNotFoundException e) {
			throw new ConnectionException(e.getMessage());
		}		
	}


	protected boolean doReceiveBoolean()
	throws ConnectionException {
		try {
			return (boolean) receive.readBoolean();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


	protected int doReceiveBytes(byte [] buffer)
	throws ConnectionException{
		try {
			return (int) receive.read(buffer);
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


	protected long doReceiveLong()
	throws ConnectionException {
		try {
			return (long) receive.readLong();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


	protected int doReceiveInt()
	throws ConnectionException {
		try {
			return (int) receive.readInt();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


	protected String doReceiveString()
	throws ConnectionException {
		try {
			return (String) receive.readUTF();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


	protected void doFlush()
	throws ConnectionException {
		try {
			send.flush();
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}		
	}
	
	
	protected void doClose()
	throws ConnectionException{

		try {
			if(socket!=null){
				socket.close();
				socket = null;
			}
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}

		try {
			if(send!=null){
				send.close();
				send = null;
			}
		}catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}

		try {
			if(receive!=null){
				receive.close();
				receive = null;
			}
		} catch (IOException e) {
			throw new ConnectionException(e.getMessage());
		}
	}


}
